import java.io.PrintWriter;

import com.yhj.servlet.container.Servlet;
import com.yhj.servlet.container.ServletRequest;
import com.yhj.servlet.container.ServletResponse;


public class Hello implements Servlet {
	
	public Hello(){
		System.out.println("constructor");
	}

	@Override
	public void doGet(ServletResponse response, ServletRequest request) {
		String uname=request.getParameter("uname");
		int age=Integer.parseInt(request.getParameter("age"));
		String body="<html><head></head><body>姓名:"+uname+"年龄:"+age+"</body></html>";
		String protocal="HTTP/1.1 200 OK\r\nContent-Type: text/html;charset=utf-8;\r\nContent-Length: "+body.getBytes().length+"\r\n\r\n"+body;
		PrintWriter out=response.getWriter();
		out.println(protocal);
		out.flush();
	}

	@Override
	public void doPost(ServletResponse response, ServletRequest request) {
		String uname=request.getParameter("uname");
		String age=request.getParameter("age");
		
		System.out.println(uname+"\t"+age);
		
		String body="{\"uname\":\""+uname+"\",\"age\":"+age+"}";
		String protocal="HTTP/1.1 200 OK\r\nContent-Type:  application/json;charset=utf-8;\r\nContent-Length: "+body.getBytes().length+"\r\n\r\n"+body;
		PrintWriter out=response.getWriter();
		out.println(protocal);
		out.flush();
		
//		PrintWriter out=
	}

	@Override
	public void init() {
		System.out.println("init()");
	}

	@Override
	public void service(ServletResponse response, ServletRequest request) {
		//判断method是什么，调用doGet或者doPost方法 
		if(request.getMethod().equals("GET")){
			doGet(response,request);
		}else if(request.getMethod().equals("POST")){
			doPost(response,request);
		}
	}

}
